package com.hex.es.service.impl;

import com.hex.es.annotation.EsBuildParamTypeInfo;
import com.hex.es.service.ExecutionLinkControllerInterface;
import com.hex.es.service.ParamBuildInterface;
import com.hex.es.service.ParamTypeCheckInterface;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 默认当成object对象处理器
 * @author: Hex
 * @date: 2019/11/13 16:29
 */
@Slf4j
@Service
public class ParamDefaultTypeCheckImpl implements ParamTypeCheckInterface {

    @Resource
    private ExecutionLinkControllerInterface controllerInterface;

    @Override
    public void handler(String paramName,Object paramValue,BoolQueryBuilder boolQueryBuilder, NativeSearchQueryBuilder searchQueryBuilder, EsBuildParamTypeInfo esBuildParamTypeInfo) throws IllegalAccessException {
        Field[] fields = paramValue.getClass().getDeclaredFields();
        for(Field field:fields) {
            field.setAccessible(true);
            controllerInterface.paramBuild(this.getClass(),field.getName(),field.get(paramValue),boolQueryBuilder,searchQueryBuilder,esBuildParamTypeInfo);
        }
    }

    @Override
    public Boolean checkType(Object object) {
        return false;
    }

    @Override
    public Map<Class, List<Class<? extends ParamBuildInterface>>> build() {
        Map<Class, List<Class<? extends ParamBuildInterface>>> maps = new HashMap<>(16);
        List<Class<? extends ParamBuildInterface>> classesList = Arrays.asList(ParamBuildPageImpl.class, ParamBuildOrderByImpl.class, ParamBuildRangeImpl.class, ParamBuildGorupByImpl.class, ParamBuildBaseImpl.class);
        maps.put(this.getClass(),classesList);
        return maps;
    }
}
